package com.trkj.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.trkj.pojo.Emp;
import com.trkj.pojo.Employ;
import com.trkj.service.EmploysService;
import com.trkj.service.WorkFlowService;
/**
 * 入职流程的控制器
 * @author Lixd
 *
 */
@Controller
@SessionAttributes({"emp", "funcIds"})
public class EmployProcessController {
	
	@Autowired
	EmploysService employsService;
	@Autowired
	WorkFlowService workFlowService;
	/**
	 * 部署流程接口
	 * @author Lixd
	 */
	@RequestMapping("/bushu_employe")
	public void deploymentProcessDefinition_classpath() {
		workFlowService.deploymentProcessDefinition_classpath("人员录用流程",
				"diagrams/employ_actProcess.png","diagrams/employ_actProcess.bpmn");
	}
	/**
	 * 获得当前任务名称
	 * @author Lixd
	 * @param taskId 从界面获取的当前任务编号
	 * @return
	 */
	@RequestMapping("/now_task_name")
	@ResponseBody
	public String now_task_name(String taskId) {
		return employsService.now_task_name(taskId);
	}
	
	/**
	 * 入职流程提交
	 * @author Lixd
	 * @param employ
	 * @param deptId
	 * @param positionId
	 * @param processDefinitionKey
	 * @return
	 */
	@RequestMapping("/submit_employs")
	@ResponseBody
	public boolean submit_employs(Employ employ, @RequestParam("deptId") Integer deptId,Model model,
			@RequestParam("positionId") Integer positionId,@RequestParam("pk")String processDefinitionKey) {
		Integer useid = getOnLineEmp(model);
		Integer processId = employsService.save_employs(useid,employ, deptId, positionId);
		if (processId!=0) {
			return workFlowService.startEmployProcess(processId, useid, processDefinitionKey);
		}
		return false;
	}
	/**
	 * 获取登录人的id
	 * @author Lixd
	 * @param model
	 * @return
	 */
	private Integer getOnLineEmp(Model model) {
		Emp emp = (Emp)model.asMap().get("emp");
		Integer useid = emp.getEmpId();
		return useid;
	}
	/**
	 * 入职流程暂存
	 * @author Lixd
	 * @param employ
	 * @param deptId
	 * @param positionId
	 * @return
	 */
	@RequestMapping("/save_employs")
	@ResponseBody
	public boolean save_employs(Employ employ, @RequestParam("deptId") Integer deptId,
			@RequestParam("positionId") Integer positionId,Model model) {
		Integer useid = getOnLineEmp(model);
		Integer isSave = employsService.save_employs(useid,employ, deptId, positionId);
		if (isSave!=0) {
			return true;
		}
		return false;
	}
	/**
	 * 表单部门下拉框数据
	 * @author Lixd
	 * @return
	 */
	@RequestMapping("/combobox_dept")
	public @ResponseBody List<Map<String, Object>> findAll_dept() {
		return employsService.findAll_dept_combobox();
	}
	/**
	 * 根据部门查询练级的职位
	 * @param deptId
	 * @return
	 */
	@RequestMapping("/combobox_position")
	public @ResponseBody List<Map<String, Object>> combobox_position(Integer deptId) {
		return employsService.findAll_position_combobox(deptId);
	}

}
